import { API } from "@/services";
import { defineStore } from "pinia";
import { ref } from "vue";
import { useTokenStore } from "../token";
export interface Course {
  name: string;
  teacher?: string;
  introduction?: string;
  syllabus?: string;
  credit?: number;
}

export interface CourseTeacher {
  name: string;
}

export interface introduction {
  content: string;
}
export interface CourseSyllabus {
  date: Blob;
}
export interface CourseCalendar {
  date: string;
  content: string;
}


export const useCourseStore = defineStore("course", () => {
  const course = ref<Course>()
  const courseTeacher = ref<CourseTeacher>()
  const courseIntroduction = ref<introduction>()
  const courseSyllabus = ref<CourseSyllabus>()

  async function dispatchGetCourseTeacher(courseId: number) {
    const tokenStore = useTokenStore();
    await API.courses.getTeacherInfo(courseId, tokenStore.token.id).then((res) => {
      if (res.status === 200) {
        courseTeacher.value = {
          name: res.data['teacher name'],
        }
      }
    }
    ).catch((err) => {
      console.log(err);
    });
  }

  async function dispatchGetCourseIntroduction(courseId: number) {
    const tokenStore = useTokenStore();
    await API.courses.getCourseInfo(courseId, tokenStore.token.id).then((res) => {
      if (res.status === 200) {
        courseIntroduction.value = {
          content: res.data['introduction'],
        };
      }
    }).catch((err) => {
      console.log(err);
    });
  }

  // 获取某课程的课程大纲
  async function dispatchGetSyllabus(courseId: number) {
    const tokenStore = useTokenStore();
    await API.courses.getSyllabus(courseId, tokenStore.token.id).then((res) => {

      if (res.status === 200) {
        courseSyllabus.value = {
          date: new Blob([res.data], { type: 'application/pdf' }),
        }
        console.log(courseSyllabus.value);
      }}).catch((err) => {
      console.log(err);
    });
  }
  return {
    course,
    courseTeacher,
    courseIntroduction,
    courseSyllabus,
    dispatchGetCourseTeacher,
    dispatchGetCourseIntroduction,
    dispatchGetSyllabus,
  }

});
